﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.WaterReadingControl.Modify;

namespace University.Workshop.Presentation.WaterReading
{
    /// <summary>
    /// Interaction logic for ModifyWaterReading.xaml
    /// </summary>
    public partial class ModifyWaterReading : Page, IModifyWaterReadingView
    {
        PresenterModifyWaterReading pModifyWaterReading;
        public ModifyWaterReading(Model.WaterReadingControl waterReding)
        {
            InitializeComponent();
            pModifyWaterReading = new PresenterModifyWaterReading(this, waterReding);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            pModifyWaterReading.UpdateViewData();
            App.Current.MainWindow.Title = "Modificar Lectura";
        }

        #region Functions
        public void NotifyMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Atencion!!!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        public void ReturnSearchWaterReading()
        {
            this.NavigationService.Navigate(new SearchWaterReading());
        }
        #endregion

        #region Fields
        public string Allotment
        {
            set
            {
                AllotmentTxt.Text = value;
            }
        }

        public string Square
        {
            set
            {
                SquareTxt.Text = value;
            }
        }

        public string Name
        {
            set
            {
                NameTxt.Text = value;
            }
        }

        public string LastName
        {
            set
            {
                LastNameTxt.Text = value;
            }
        }

        public double PreviousReading 
        {
            set
            {
                PreviousReadTxt.Text = value.ToString();
            }
             
        }
        
        public double CurrentReading
        {
            set 
            {
                if (value == 0)
                    CurrentReadTxt.Text = string.Empty;
                else
                    CurrentReadTxt.Text = value.ToString();
            }

            get
            {
                if (CurrentReadTxt.Text != string.Empty)
                    return (Convert.ToDouble(CurrentReadTxt.Text));
                else
                    return 0;
            }
        }

        public decimal PaymentAmount 
        { 
            set
            {
                if (value == 0)
                    PaymentAmountTxt.Text = string.Empty;
                else
                    PaymentAmountTxt.Text = value.ToString();
            }
            get
            {
                if (PaymentAmountTxt.Text != string.Empty)
                    return Convert.ToDecimal(PaymentAmountTxt.Text);
                else
                    return 0;
            }
        }
        #endregion

        #region Buttons
        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new SearchWaterReading());
        }

        private void CalculateBtn_Click(object sender, RoutedEventArgs e)
        {
            pModifyWaterReading.CalculatePaymentAmount();
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            pModifyWaterReading.ModifyWaterReading();
        }
        #endregion

        private void CurrentReadTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch)  || ch.Equals('.'));
            }
        }



        

        

    }
}
